package com.study.demo01.err;

import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * @author zhuzi
 * 2023/9/12 16:48
 */
@Slf4j
public class Demo10 {

    @Test
    public void arrToList() {
        int[] arr = {1, 2, 3};
        //此方法传入的是 泛型T类型可变参数，最终int 数组作为一个整体成了泛型类型T
        List<int[]> ints = Arrays.asList(arr);

        // boxed将数值流转为Stream
        List<Integer> collect = Arrays.stream(arr).boxed().collect(Collectors.toList());

        Integer[] arr2 = {1, 2, 3};
        List list2 = Arrays.asList(arr2);
        log.info("list:{} size:{} class:{}", list2, list2.size(), list2.get(0).getClass());

    }

    @Test
    public void testGet() {
        String[] arr = {"1", "2", "3"};
        List list = Arrays.asList(arr);
        //第三个坑，对原始数组的修改会影响到我们获得的那个 List
        arr[1] = "4";
        try {
            //第二个坑，Arrays.asList 返回的 List 不支持增删操作
            list.add("5");
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        log.info("arr:{} list:{}", Arrays.toString(arr), list);
    }

    @Test
    public void subList() {
        ArrayList<String> list = Lists.newArrayList("a", "b", "c", "d", "e");
        //list.subList 返回的子 List 不是一个普通的 ArrayList。这个子 List 可以认为是原始 List 的视图，会和原始 List 相互影响
        List<String> subList = list.subList(0, list.size());
        System.out.println(subList);

        subList.clear();
        System.out.println(list);
        System.out.println(subList);
    }

    @Test
    public void testSubList2() {
        List<Integer> list = IntStream.rangeClosed(1, 10).boxed().collect(Collectors.toList());
        List<Integer> subList = list.subList(1, 4);
        System.out.println(subList);
        subList.remove(1);
        System.out.println(list);
        list.add(0);
        try {
            subList.forEach(System.out::println);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Test
    public void map() {
        //自动完成的索引，Key是用户输入的部分用户名，Value是对应的用户数据
        ConcurrentHashMap<String, List<String>> map = new ConcurrentHashMap<>();
        map.put("a", Lists.newArrayList("a"));
        map.put("b", Lists.newArrayList("a", "b"));

        //map.computeIfAbsent()
        //map.computeIfPresent()


    }
}
